//
//  InsideWallView.m
//  Model
//
//  Created by zeb-apple on 2017/5/26.
//  Copyright © 2017年 YDJH. All rights reserved.
//  室内墙面载体

#import "InsideWallView.h"
#import "HZFurniture_Header.h"
#import "HZFurniture.h"
#import "HZInsideWall.h"
#import <objc/message.h>

@interface InsideWallView ()

@property (nonatomic, strong) NSMutableArray *pool; //重用池

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, copy) FurnitureClickBlock  furnitureClickBlock;
@property (nonatomic, copy) GoOutBlock goOutBlock;

@end

@implementation InsideWallView

- (instancetype)initWithFrame:(CGRect)frame furnitureClickBlock:(FurnitureClickBlock)furnitureClickBlock goOutBlock:(GoOutBlock)goOutBlock {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.furnitureClickBlock = furnitureClickBlock;
        self.goOutBlock = goOutBlock;
        [self initView];
    }
    return self;
}
- (void)initView {
    
    UIImage *image = [UIImage imageNamed:@"returnBuildOutside"];
    self.goOutImageView.image = image;
    self.goOutImageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    [self addSubview:self.goOutImageView];
}
- (NSMutableArray *)pool {
    if (!_pool) {
        _pool = [NSMutableArray array];
    }
    return _pool;
}
- (UIImageView *)goOutImageView {
    if (!_goOutImageView) {
        _goOutImageView = [[UIImageView alloc] init];
        _goOutImageView.backgroundColor = [UIColor brownColor];
        _goOutImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *goOut = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goOut)];
        [_goOutImageView addGestureRecognizer:goOut];
    }
    return _goOutImageView;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.goOutImageView.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)-self.goOutImageView.image.size.height/2);
}
//刷新视图
- (void)reloadWithInsideWall:(HZInsideWall *)insideWall {
    
    __weak typeof(self) weakSelf = self;
    //重置家具视图
    [self resetSubviewsWithClass:[InsideFurnitureView class]];
    //根据数据源家具model添加家具
    for (HZFurniture *furniture in insideWall.furnitureArr) {
        
        InsideFurnitureView *furnitureView = (InsideFurnitureView *)[self goBackViewFromPoolWithClass:[InsideFurnitureView class]];
                
        furnitureView.furniture = furniture;
        furnitureView.furnitureClickBlock = ^(HZFurniture *furniture, UIImageView *superView) {
            weakSelf.furnitureClickBlock(furniture, superView);
        };
        [self addSubview:furnitureView];
    }
}
// 根据class返回未使用的view
- (UIView *)goBackViewFromPoolWithClass:(Class)class {
    
    __block UIView *view = nil;
    [self.pool enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:class] && !obj.haveuse) {
            *stop = YES;
            view = obj;
            view.haveuse = YES;
        }

    }];
    if (!view) {
        view = [[class alloc] init];
        view.haveuse = YES;
        [self addSubview:view];
        //新增加的添加到缓存池
        [self.pool addObject:view];
    }
    return view;
}
//将子视图的 haveuse 重置
- (void)resetSubviewsWithClass:(Class)class {
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:class]) {
            view.haveuse = NO;
            [view removeFromSuperview];
        }
    }
}
#pragma mark
#pragma mark action
- (void)goOut {
    self.goOutBlock(self);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end



@implementation UIView (Room_Property)

- (void)setHaveuse:(BOOL)haveuse {
    // 动态添加属性的本质是:让对象的某个属性与值产生关联
    objc_setAssociatedObject(self, @selector(haveuse), @(haveuse), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)haveuse {
    // 获取对应属性的值
    return [objc_getAssociatedObject(self, @selector(haveuse)) boolValue];
}


@end
